package org.wu.framework.authorization.platform.infrastructure.persistence;

import org.wu.framework.authorization.platform.infrastructure.entity.SysZoneDO;
import org.wu.framework.authorization.platform.infrastructure.converter.SysZoneConverter;
import org.wu.framework.authorization.platform.infrastructure.mapper.SysZoneMapper;
import org.wu.framework.authorization.platform.domain.model.sys.zone.SysZoneRepository;
import org.springframework.stereotype.Repository;

import java.util.stream.Collectors;

import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import org.wu.framework.authorization.platform.domain.model.sys.zone.SysZone;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe 系统区域
 *
 * @author Jia wei Wu
 * @date 2025/07/27 03:22 下午
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SysZoneRepositoryImpl implements SysZoneRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    /**
     * describe 新增系统区域
     *
     * @param sysZone 新增系统区域
     * @return {@link Result<SysZone>} 系统区域新增后领域对象
     * @author Jia wei Wu
     * @date 2025/07/27 03:22 下午
     **/

    @Override
    public Result<SysZone> story(SysZone sysZone) {
        SysZoneDO sysZoneDO = SysZoneConverter.INSTANCE.fromSysZone(sysZone);
        lazyLambdaStream.upsert(sysZoneDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增系统区域
     *
     * @param sysZoneList 批量新增系统区域
     * @return {@link Result<List<SysZone>>} 系统区域新增后领域对象集合
     * @author Jia wei Wu
     * @date 2025/07/27 03:22 下午
     **/

    @Override
    public Result<List<SysZone>> batchStory(List<SysZone> sysZoneList) {
        List<SysZoneDO> sysZoneDOList = sysZoneList.stream().map(SysZoneConverter.INSTANCE::fromSysZone).collect(Collectors.toList());
        lazyLambdaStream.upsert(sysZoneDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个系统区域
     *
     * @param sysZone 查询单个系统区域
     * @return {@link Result<SysZone>} 系统区域领域对象
     * @author Jia wei Wu
     * @date 2025/07/27 03:22 下午
     **/

    @Override
    public Result<SysZone> findOne(SysZone sysZone) {
        SysZoneDO sysZoneDO = SysZoneConverter.INSTANCE.fromSysZone(sysZone);
        SysZone sysZoneOne = lazyLambdaStream.selectOne(
                LazyWrappers.<SysZoneDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysZoneDO::getCode, sysZoneDO.getCode())
                        .eqIgnoreEmpty(SysZoneDO::getCreateTime, sysZoneDO.getCreateTime())
                        .eqIgnoreEmpty(SysZoneDO::getCreateUserId, sysZoneDO.getCreateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getIcon, sysZoneDO.getIcon())
                        .eqIgnoreEmpty(SysZoneDO::getId, sysZoneDO.getId())
                        .eqIgnoreEmpty(SysZoneDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysZoneDO::getName, sysZoneDO.getName())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateTime, sysZoneDO.getUpdateTime())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateUserId, sysZoneDO.getUpdateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getUrl, sysZoneDO.getUrl())
                , SysZone.class);
        return ResultFactory.successOf(sysZoneOne);
    }

    /**
     * describe 查询多个系统区域
     *
     * @param sysZone 查询多个系统区域
     * @return {@link Result<List<SysZone>>} 系统区域领域对象
     * @author Jia wei Wu
     * @date 2025/07/27 03:22 下午
     **/

    @Override
    public Result<List<SysZone>> findList(SysZone sysZone) {
        SysZoneDO sysZoneDO = SysZoneConverter.INSTANCE.fromSysZone(sysZone);
        List<SysZone> sysZoneList = lazyLambdaStream.selectList(
                LazyWrappers.<SysZoneDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysZoneDO::getCode, sysZoneDO.getCode())
                        .eqIgnoreEmpty(SysZoneDO::getCreateTime, sysZoneDO.getCreateTime())
                        .eqIgnoreEmpty(SysZoneDO::getCreateUserId, sysZoneDO.getCreateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getIcon, sysZoneDO.getIcon())
                        .eqIgnoreEmpty(SysZoneDO::getId, sysZoneDO.getId())
                        .eqIgnoreEmpty(SysZoneDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysZoneDO::getName, sysZoneDO.getName())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateTime, sysZoneDO.getUpdateTime())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateUserId, sysZoneDO.getUpdateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getUrl, sysZoneDO.getUrl())
                , SysZone.class);
        return ResultFactory.successOf(sysZoneList);
    }

    /**
     * describe 分页查询多个系统区域
     *
     * @param size    当前页数
     * @param current 当前页
     * @param sysZone 分页查询多个系统区域
     * @return {@link Result<LazyPage<SysZone>>} 分页系统区域领域对象
     * @author Jia wei Wu
     * @date 2025/07/27 03:22 下午
     **/

    @Override
    public Result<LazyPage<SysZone>> findPage(int size, int current, SysZone sysZone) {
        SysZoneDO sysZoneDO = SysZoneConverter.INSTANCE.fromSysZone(sysZone);
        LazyPage<SysZone> lazyPage = new LazyPage<>(current, size);
        LazyPage<SysZone> sysZoneLazyPage = lazyLambdaStream.selectPage(
                LazyWrappers.<SysZoneDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysZoneDO::getCode, sysZoneDO.getCode())
                        .eqIgnoreEmpty(SysZoneDO::getCreateTime, sysZoneDO.getCreateTime())
                        .eqIgnoreEmpty(SysZoneDO::getCreateUserId, sysZoneDO.getCreateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getId, sysZoneDO.getId())
                        .eqIgnoreEmpty(SysZoneDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysZoneDO::getName, sysZoneDO.getName())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateTime, sysZoneDO.getUpdateTime())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateUserId, sysZoneDO.getUpdateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getUrl, sysZoneDO.getUrl())
                , lazyPage, SysZone.class);
        return ResultFactory.successOf(sysZoneLazyPage);
    }

    /**
     * describe 删除系统区域
     *
     * @param sysZone 删除系统区域
     * @return {@link Result<SysZone>} 系统区域
     * @author Jia wei Wu
     * @date 2025/07/27 03:22 下午
     **/

    @Override
    public Result<SysZone> remove(SysZone sysZone) {
        SysZoneDO sysZoneDO = SysZoneConverter.INSTANCE.fromSysZone(sysZone);
        lazyLambdaStream.delete(
                LazyWrappers.<SysZoneDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysZoneDO::getCode, sysZoneDO.getCode())
                        .eqIgnoreEmpty(SysZoneDO::getCreateTime, sysZoneDO.getCreateTime())
                        .eqIgnoreEmpty(SysZoneDO::getCreateUserId, sysZoneDO.getCreateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getIcon, sysZoneDO.getIcon())
                        .eqIgnoreEmpty(SysZoneDO::getId, sysZoneDO.getId())
                        .eqIgnoreEmpty(SysZoneDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysZoneDO::getName, sysZoneDO.getName())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateTime, sysZoneDO.getUpdateTime())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateUserId, sysZoneDO.getUpdateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getUrl, sysZoneDO.getUrl())
        );
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在系统区域
     *
     * @param sysZone 系统区域领域对象
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在
     * @author Jia wei Wu
     * @date 2025/07/27 03:22 下午
     **/

    @Override
    public Result<Boolean> exists(SysZone sysZone) {
        SysZoneDO sysZoneDO = SysZoneConverter.INSTANCE.fromSysZone(sysZone);
        Boolean exists = lazyLambdaStream.exists(
                LazyWrappers.<SysZoneDO>lambdaWrapper()
                        .eqIgnoreEmpty(SysZoneDO::getCode, sysZoneDO.getCode())
                        .eqIgnoreEmpty(SysZoneDO::getCreateTime, sysZoneDO.getCreateTime())
                        .eqIgnoreEmpty(SysZoneDO::getCreateUserId, sysZoneDO.getCreateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getIcon, sysZoneDO.getIcon())
                        .eqIgnoreEmpty(SysZoneDO::getId, sysZoneDO.getId())
                        .eqIgnoreEmpty(SysZoneDO::getIsDeleted, false)
                        .eqIgnoreEmpty(SysZoneDO::getName, sysZoneDO.getName())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateTime, sysZoneDO.getUpdateTime())
                        .eqIgnoreEmpty(SysZoneDO::getUpdateUserId, sysZoneDO.getUpdateUserId())
                        .eqIgnoreEmpty(SysZoneDO::getUrl, sysZoneDO.getUrl())
        );
        return ResultFactory.successOf(exists);
    }

}